package com.zhou.springcloud.provider.controller;

import com.alibaba.fastjson.JSONPObject;
import com.netflix.discovery.EurekaClient;
import com.zhou.springcloud.api.pojo.Dept;
import com.zhou.springcloud.provider.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhouyongjun
 * @Description: 提供RESTFUL 服务
 * @date 2021/3/20 11:38
 */
@RestController
public class DeptController {
    @Autowired
    IDeptService deptService;
    //h获取一些配置信息，得到具体微服务
    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        return deptService.queryById(id);
    }
    @PostMapping("/dept/add")
    public boolean addDept(Dept dept) {
        return deptService.addDept(dept);
    }
    @GetMapping("dept/list")
    public List<Dept> getAll() {
        try {
            List<Dept> list = deptService.queryAll();
            System.out.println(list);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
        * @Description: 注册进来的微服务，获取一些信息
        * @author  zhouyongjun
    　　* @date 2021/3/23 8:37
    　　* @param []
    　　* @return java.lang.Object
    　　* @throws
    */
    @GetMapping("/dept/discovery")
    public Object discovery() {
        //得到微服务服务列表
        List<String> list = discoveryClient.getServices();
        System.out.println("discovery ==> services:"+list);
        //得到具体的微服务信息，通过微服务ID
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance serviceInstance : serviceInstances) {
            System.out.println(serviceInstance.getHost()+"\t"+serviceInstance.getPort()
                    +"\t"+serviceInstance.getUri()+"\t"+serviceInstance.getServiceId());
        }
        return this.discoveryClient;
    }
}
